﻿#include "OiPCH.hpp"
#include <QGraphicsScene>
#include <QtCharts/QValueAxis>
#include <QCursor>

using namespace QtCharts;

namespace Oi
{
    namespace PLScannerCore
    {
        class ProfileViewImpl : public Impl<ProfileView>
        {
        public:
            void init();
            void exit();

        public:
            ProfilesChart* _chart;
            QGraphicsScene* _scene;

        public:
            Qt::CursorShape _cursor;
            bool _isDrag;
            QPoint _dragPoint;

//         public:
//             RoiRect* _roiItem;
        };

        void ProfileViewImpl::init()
        {
            OI_Q(ProfileView);

            _isDrag = false;

            _cursor = Qt::ArrowCursor;

            _scene = new QGraphicsScene();
            _scene->setBackgroundBrush(PLScannerStyle::ChartBgColor());
            q->setScene(_scene);
            q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            q->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

            _chart = new ProfilesChart();
            _chart->legend()->hide();
            _chart->setFlag(QGraphicsItem::ItemIsSelectable, true);

            QValueAxis* pXAxis = new QValueAxis();
            pXAxis->setRange(-100, 100);
            pXAxis->setGridLinePen(QColor::fromRgb(50, 50, 50));
            _chart->setAxisX(pXAxis);

            QValueAxis* pYAxis = new QValueAxis();
            pYAxis->setRange(0, 10);
            pYAxis->setGridLinePen(QColor::fromRgb(50, 50, 50));
            _chart->setAxisY(pYAxis);

            _chart->layout()->setContentsMargins(0, 0, 0, 0);
            _chart->setBackgroundRoundness(0);

//             _chart->axisX()->setGridLineVisible(true);
//             _chart->axisY()->setGridLineVisible(true);
//             _chart->axisX()->setMinorGridLineVisible(true);

            _scene->addItem(_chart);

//            _roiItem = new RoiRect(q);
//            _scene->addItem(_roiItem);
//            _roiItem->setVisible(_roiItem->_displayRoi);

            _scene->setSceneRect(1.0 * INT_MIN, 1.0 * INT_MIN, 2.0 * INT_MAX, 2.0 * INT_MAX);

            q->setRenderHint(QPainter::Antialiasing);

            q->setDragMode(QGraphicsView::RubberBandDrag);
            q->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
        }

        void ProfileViewImpl::exit()
        {

        }

        ProfileView::ProfileView(QWidget *parent)
            : QGraphicsView(parent)
        {
            OI_I(ProfileView)->init();
        }

        ProfileView::~ProfileView()
        {
            OI_I(ProfileView)->exit();
        }

        ProfilesChart* ProfileView::chart()
        {
            return OI_IMPL()->_chart;
        }

        void ProfileView::zoomFit()
        {
            OI_F(ProfileView);
            d->_chart->setGeometry(0, 0, viewport()->width(), viewport()->height());
            fitInView(d->_chart, Qt::KeepAspectRatio);
        }

        void ProfileView::resizeEvent(QResizeEvent *event)
        {
            zoomFit();
        }

        void ProfileView::mousePressEvent(QMouseEvent *event)
        {
            OI_F(ProfileView);
            if(event->button() & Qt::MiddleButton)
            {
                d->_cursor = this->cursor().shape();
                setCursor(Qt::OpenHandCursor);
                d->_isDrag = true;
                d->_dragPoint = event->pos();

                event->accept();
                return;
            }

            QGraphicsView::mousePressEvent(event);
        }

        void ProfileView::mouseMoveEvent(QMouseEvent* event)
        {
            OI_F(ProfileView);

            if( event->buttons() & Qt::MiddleButton )
            {
                if(d->_isDrag)
                {
                    d->_chart->scroll(d->_dragPoint.x() - event->x(), event->y() - d->_dragPoint.y());
                    d->_dragPoint = event->pos();

                    event->accept();

                    return;
                }
            }

            QGraphicsView::mouseMoveEvent(event);
        }

        void ProfileView::mouseReleaseEvent(QMouseEvent* event)
        {
            OI_F(ProfileView);

            if(event->button() & Qt::MiddleButton)
            {
                setCursor(d->_cursor);
                d->_isDrag = false;

                event->accept();
                return;
            }

            QGraphicsView::mouseReleaseEvent(event);
        }

        void ProfileView::mouseDoubleClickEvent(QMouseEvent* event)
        {
            OI_F(ProfileView);

            if (event->button() & Qt::MouseEventCreatedDoubleClick)
            {
                d->_chart->autoRange();
            }

            QGraphicsView::mouseDoubleClickEvent(event);
        }

        void ProfileView::paintEvent(QPaintEvent *event)
        {
            QGraphicsView::paintEvent(event);
        }

        void ProfileView::wheelEvent(QWheelEvent *event)
        {
            OI_F(ProfileView);
            if(event->delta() > 0)
            {
                d->_chart->zoom(1.1);
            }
            else
            {
                d->_chart->zoom(10.0 / 11);
            }
        }
    }
}
